package com.liangzhong.proxy.chat
{
	import com.common.stdlib.StringUtil;
	import com.liangzhong.commandconst.chat.SMDChat;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.pub.CMD_Public;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.chat.vo.ChatVo;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxyconstant.chat.ChatProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.utils.ByteArray;
	
	public class ChatProxy extends BaseProxy
	{
		public static const PROXYNAME:String = "chat_proxyname";
		public function ChatProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
		}
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Public.MAIN://公用协议
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				case MainCommandType.INTERACTIVE:
					if(subCmdID == SMDChat.SEND_MESSAGE)
					{
						getMessage(buffer);
						return true;
					}
						
//					return onDeal(subCmdID,buffer,dataSize,clientSocket);
					
//				case MainCommandType.MAINDEAL:
//					return onMainDeal(subCmdID,buffer,dataSize,clientSocket);
			}
			return false;
		}
		/**
		 * 处理
		 * 公共协议类型 40
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var results:int;
			switch(subCmdID)
			{
//				case SMDChat.SEND_MESSAGE:
//					getMessage(buffer);
//					return true;
				case SMDChat.GET_NOTICE:
					getNotic(buffer);
					return true;
			}
			return false;
		}
		/**
		 *发送聊天信息 
		 * @param $obj
		 * 
		 */
		public function requestSendMessage($obj:Object):void
		{
			var chatVo:ChatVo = $obj.vo;
			
			var len:int = StringUtil.getStringByteLength(chatVo.name);
			
			len += StringUtil.getStringByteLength(chatVo.pFormat);
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + len + 9 ,MainCommandType.INTERACTIVE,SMDChat.SEND_MESSAGE);
			sendBuf.writeByte(chatVo.ntype);
			sendBuf.writeUTF(chatVo.name);
			sendBuf.writeInt(chatVo.id)
			sendBuf.writeUTF(chatVo.pFormat);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *获取聊天信息 
		 * 
		 */
		private function getMessage(buffer:ByteArray):void
		{
			AppData.getInstance().chatDataManager.getChatMessage(buffer);
		}
		/**
		 *获得公告 
		 * @param buffer
		 * 
		 */
		private function getNotic(buffer:ByteArray):void
		{
			
			
			var arr:Array = AppData.getInstance().chatDataManager.handerNotic(buffer);
			this.sendNotification(ChatProxyConstant.GET_NOTIC,{array:arr});
			
		}
	}
}